package com.duubee.btframe.utils;

import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Scanner;

public class BtSystemUtil {


    private static OperatingSystemMXBean operatingSystemMXBean = null;

    /**
     * 获取mac地址
     * @return mac地址
     */
    public static String getMac() {
        try {
            Enumeration<NetworkInterface> el = NetworkInterface
                    .getNetworkInterfaces();
            while (el.hasMoreElements()) {
                byte[] mac = el.nextElement().getHardwareAddress();
                if (mac == null)
                    continue;
                return BtStringUtil.getSplitString(BtByteUtil.bytesToHexString(mac), "-", 2).toUpperCase();
            }
        } catch (Exception exception) {
        }
        return BtStringUtil.empty;
    }

    /**
     * 获取Cpu序列号
     * @return
     */
    public static String getCpuId() {
        try {
            Process process = Runtime.getRuntime().exec(
                    new String[]{"wmic", "cpu", "get", "ProcessorId"});
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String property = sc.next();
            String serial = sc.next();
            return serial;
        }catch (Exception e){
        }
        return BtStringUtil.empty;
    }

    /**
     * 获取内存物理大小
     * @return 单位 (Bytes)
     */
    public static long getTotalPhysicalMemorySize(){
        if(operatingSystemMXBean == null){
            operatingSystemMXBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
        }
        return operatingSystemMXBean.getTotalPhysicalMemorySize();
    }

    public static void main(String[] args) {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

        long totalVirtualMemory = operatingSystemMXBean.getTotalSwapSpaceSize();
        System.out.println(operatingSystemMXBean.getName());
        System.out.println(operatingSystemMXBean.getVersion());
        System.out.println(operatingSystemMXBean.getSystemLoadAverage());
        System.out.println(operatingSystemMXBean.getTotalPhysicalMemorySize());

        System.out.println(totalVirtualMemory);
    }

    /**
     * 获取本地IP地址
     * @return
     * @throws SocketException
     */
    public static String getLocalIpAddr() throws SocketException {

        String clientIP = null;
        Enumeration<NetworkInterface> networks = null;
        //获取所有网卡设备
        networks = NetworkInterface.getNetworkInterfaces();
        if (networks == null) {
            //没有网卡设备 打印日志  返回null结束
            return null;
        }

        InetAddress ip;
        Enumeration<InetAddress> addrs;
        // 遍历网卡设备
        while (networks.hasMoreElements()) {
            NetworkInterface ni = networks.nextElement();
            //过滤掉 loopback设备、虚拟网卡
            if (!ni.isUp() || ni.isLoopback() || ni.isVirtual()) {
                continue;
            }

            addrs = ni.getInetAddresses();
            if (addrs == null) {
                continue;
            }
            // 遍历InetAddress信息
            while (addrs.hasMoreElements()) {
                ip = addrs.nextElement();
                if (!ip.isLoopbackAddress() && ip.isSiteLocalAddress() && ip.getHostAddress().indexOf(":") == -1) {
                    try {
                        clientIP = ip.toString().split("/")[1];
                    } catch (ArrayIndexOutOfBoundsException e) {
                        clientIP = null;
                    }
                }
            }
        }
        return clientIP;
    }
}
